package com.vanzo.talkie;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.vanzo.bean.Channel;
import com.vanzo.bean.ChannelDaoUtil;
import com.vanzo.talkie2.R;

/**
 * Created by tisen on 2018/1/22.
 */

public class ChannelEditActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = ChannelEditActivity.class.getSimpleName();
    public static final String NEW_CHANNEL_ACTION = "new_channel_action";
    public static final String VIEW_CHANNEL_ACTION = "view_channel_action";
    public static final String EDIT_CHANNEL_ACTION = "edit_channel_action";


    private TextView cancel, topTitle, save;
    private EditText title;
    private TextView tfv, rfv, tx, rx, sq;

    private FrPicker tFrPicker;
    private FrPicker rFrPicker;
    private CxPicker tCxPicker;
    private CxPicker rCxPicker;
    private VolumPicker volumPicker;

    private Channel channel;

    private ChannelDaoUtil channelDaoUtil;
    private String action;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channel_edit);
        channelDaoUtil = new ChannelDaoUtil(this);

        cancel = (TextView) findViewById(R.id.channel_top_cancel);
        topTitle = (TextView) findViewById(R.id.channel_top_title);
        save = (TextView) findViewById(R.id.channel_top_save);
        title = (EditText) findViewById(R.id.channel_edit_title_edit);
        tfv = (TextView) findViewById(R.id.channel_edit_tfv_edit);
        rfv = (TextView) findViewById(R.id.channel_edit_rfv_edit);
        tx = (TextView) findViewById(R.id.channel_edit_tx_edit);
        rx = (TextView) findViewById(R.id.channel_edit_rx_edit);
        sq = (TextView) findViewById(R.id.channel_edit_sq_edit);
        cancel.setOnClickListener(this);
        save.setOnClickListener(this);

        action = getIntent().getAction();
        Log.d(TAG, "action = " + action);
        if (action.equals(NEW_CHANNEL_ACTION)) {
            tFrPicker = new FrPicker(this, tfv, "400.0000");
            rFrPicker = new FrPicker(this, rfv, "400.0000");
            tCxPicker = new CxPicker(this, tx, "00");
            rCxPicker = new CxPicker(this, rx, "00");
            volumPicker = new VolumPicker(this, sq, "0");
            save.setVisibility(View.VISIBLE);
        } else {
            Long id = getIntent().getLongExtra("id", 0);
            channel = application.getChannelById(id);
            title.setText(channel.getTitle());
            tFrPicker = new FrPicker(this, tfv, channel.getTfv());
            rFrPicker = new FrPicker(this, rfv, channel.getRfv());
            tCxPicker = new CxPicker(this, tx, channel.getTx());
            rCxPicker = new CxPicker(this, rx, channel.getRx());
            volumPicker = new VolumPicker(this, sq, channel.getSq());
            if (action.equals(VIEW_CHANNEL_ACTION)) {
                title.setClickable(false);
                title.setFocusable(false);
                tfv.setClickable(false);
                rfv.setClickable(false);
                tx.setClickable(false);
                rx.setClickable(false);
                sq.setClickable(false);
                save.setVisibility(View.INVISIBLE);
            } else {

            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.channel_top_cancel:
                finish();
                break;
            case R.id.channel_top_save:
                if (title.getText().toString().trim().isEmpty()) {
                    Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (channel == null) {
                        channel = new Channel();
                        channel.setTitle(title.getText().toString());
                        channel.setTfv(tfv.getText().toString());
                        channel.setRfv(rfv.getText().toString());
                        channel.setTx(tx.getText().toString());
                        channel.setRx(rx.getText().toString());
                        channel.setSq(sq.getText().toString());
                        application.addChannel(channel);
                    } else {
                        channel.setTitle(title.getText().toString());
                        channel.setTfv(tfv.getText().toString());
                        channel.setRfv(rfv.getText().toString());
                        channel.setTx(tx.getText().toString());
                        channel.setRx(rx.getText().toString());
                        channel.setSq(sq.getText().toString());
                        application.updateChannel(channel);
                        finish();
                    }
                }
                break;
        }
    }
}